<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>青芽医疗管理平台</title>
    <meta name="description" content="数据采集分析系统">
    <meta name="keywords" content="数据采集分析系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
</head>
<body class="hold-transition">

<div id="app">
    <div class="filter-container">
        <el-input placeholder="角色名称" style="width: 200px;" class="filter-item" v-model="pagination.queryName"></el-input>
        <el-button @click="_getRolePage()" class="dalfBut">查询</el-button>
        <el-button type="primary" class="butT" @click="handleAdd()">新建角色</el-button>
    </div>

    <el-table size="small" current-row-key="roleId" :data="list" stripe highlight-current-row>
        <el-table-column type="index" align="center" label="序号"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center"></el-table-column>
        <el-table-column prop="roleDescription" label="角色描述" align="center"></el-table-column>
        <el-table-column prop="departmentName" label="科室名称" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button type="success" size="mini" @click="handleRole(scope.row)">分配</el-button>
                <el-button size="mini" type="danger" @click="delRole(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
    </div>

    <div class="add-form">
        <el-dialog title="新建角色" :visible.sync="dialogFormVisible"  width="360px" :close-on-click-modal="false">
            <el-form ref="dataAddForm" :model="role"  label-position="right" label-width="100px">
                <el-row>
                    <el-form-item label="角色名称" prop="roleName">
                        <el-input v-model="role.roleName"/>
                    </el-form-item>
                    <el-form-item label="角色描述" prop="roleDescription">
                        <el-input v-model="role.roleDescription"></el-input>
                    </el-form-item>
                    <el-form-item label="选择科室" prop="departmentId">
                        <el-select v-model="role.departmentId" placeholder="请选择">
                            <el-option
                                    v-for="item in departmentList"
                                    :key="item.departmentId"
                                    :label="item.departmentName"
                                    :value="item.departmentId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="addRole()">确定</el-button>
            </div>
        </el-dialog>
    </div>

    <div class="add-form">
        <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit"  width="360px" :close-on-click-modal="false">
            <el-form ref="dataEditForm" :model="role"  label-position="right" label-width="100px">
                <el-row>
                    <el-form-item label="角色名称" prop="roleName">
                        <el-input v-model="role.roleName"/>
                    </el-form-item>
                    <el-form-item label="角色描述" prop="roleDescription">
                        <el-input v-model="role.roleDescription"></el-input>
                    </el-form-item>
                    <el-form-item label="选择科室" prop="departmentId">
                        <el-select v-model="role.departmentId" placeholder="请选择">
                            <el-option
                                    v-for="item in departmentList"
                                    :key="item.departmentId"
                                    :label="item.departmentName"
                                    :value="item.departmentId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                <el-button type="primary" @click="updateRole()">确定</el-button>
            </div>
        </el-dialog>
    </div>

    <div class="add-form">
        <el-dialog title="分配权限" :visible.sync="dialogFormRole" :close-on-click-modal="false">
            <el-table ref="multipleTable" size="small" current-row-key="menuId" :data="menuList" stripe highlight-current-row @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="menuName" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="menuIcon" label="图标" align="center"></el-table-column>
            </el-table>

            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormRole=false">取消</el-button>
                <el-button type="primary" @click="addMenuRole()">提交</el-button>
            </div>
        </el-dialog>
    </div>
</div>

</body>
<!-- 引入组件库 -->
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script type="application/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            list:[],
            role:{},
            departmentList:[],
            multipleSelection:[],
            dialogFormRole:false,
            dialogFormVisible:false,
            dialogFormVisible4Edit:false,
            selectedMenus:[],
            menuList:[],
            roleId:''
        },
        mounted () {
           this.getRolePage();
        },
        methods: {
            addMenuRole(){
                axios.post('/role/addMenuRole?roleId='+this.roleId+"&menuList="+this.multipleSelection).then(res => {
                    if(res.data){
                        this.dialogFormRole=false;
                        this.$message({
                            type:'success',
                            message:"分配成功"
                        });
                    }else{
                        this.$message.error("分配失败");
                    }
                });
            },
            handleSelectionChange(val){
                this.multipleSelection=[];
                val.forEach(e => {
                    this.multipleSelection.push(e.menuId);
                })
            },
            handleRole(row){
                this.dialogFormRole=true;
                this.roleId=row.roleId;
                this.multipleSelection=[];
                axios.get("/menu/getMenuByRole?roleId="+row.roleId).then((res)=>{
                    this.selectedMenus=res.data.menuByRoleId;
                    this.menuList=res.data.menuList;
                    this.selectedMenus.forEach(row => {
                        this.$nextTick(function () {
                            this.menuList.forEach(p=>{
                                if(p.menuId==row.menuId){
                                    this.$refs.multipleTable.toggleRowSelection(p,true);
                                }
                            });
                        });
                    });
                });
            },
            updateRole(){
                this.$confirm("你确定要修改当前数据吗？","提示",{//确认框
                    type:'warning'
                }).then(()=>{
                    axios.post("/role/updateRole","role="+JSON.stringify(this.role)).then((res) => {
                        if(res){
                            this.$message({
                                type:'success',
                                message:"修改成功"
                            });
                            //重新进行分页查询
                            this.getRolePage();
                        }else{
                            //执行失败
                            this.$message.error("修改失败");
                        }
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                }).catch(()=>{
                    this.$message({
                        type:'info',
                        message:'操作已取消'
                    });
                }).finally(() => {
                    //不管成功还是失败，都调用分页查询方法
                    this.getRolePage();
                    //隐藏编辑窗口
                    this.dialogFormVisible4Edit = false;
                });
            },
            handleUpdate(row){
                this.dialogFormVisible4Edit=true;
                axios.get("/role/getRoleById?id="+row.roleId).then((res) => {
                    this.role = res.data;
                });
                axios.get("/department/getDepartmentList").then((res) => {
                    this.departmentList = res.data;
                });
            },
            delRole(row){
                this.$confirm("你确定要删除当前数据吗？","提示",{//确认框
                    type:'warning'
                }).then(()=>{
                    //用户点击确定按钮，发送ajax请求，将检查项ID提交到Controller进行处理
                    axios.post("/role/delRole","role="+JSON.stringify(row)).then((res) => {
                        if(res.data){
                            //执行成功
                            //弹出成功提示信息
                            this.$message({
                                type:'success',
                                message:"删除成功"
                            });
                            //重新进行分页查询
                            this.getRolePage();
                        }else{
                            //执行失败
                            this.$message.error("删除失败");
                        }
                    }).catch((r)=>{
                        this.showMessage(r);
                    });
                }).catch(()=>{
                    this.$message({
                        type:'info',
                        message:'操作已取消'
                    });
                });
            },

            _getRolePage(){
                this.pagination.currentPage=1;
                this.getRolePage();
            },
            getRolePage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                var queryPageBean = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryTerm: {roleName:this.pagination.queryName}
                };
                axios.post("/role/getRolePage","queryPageBean="+JSON.stringify(queryPageBean)).then((res)=>{
                    this.list = res.data.roleList;
                    this.pagination.total = res.data.total;
                });
            },
            handleCurrentChange(currentPage) {
                //为模型数据赋值（当前页面）
                this.pagination.currentPage = currentPage;
                this.getRolePage();
            },
            handleAdd(){
               this.role={};
               this.dialogFormVisible=true;
               axios.get("/department/getDepartmentList").then(res=>{
                    this.departmentList=res.data;
               });
            },
            addRole(){
                var role = JSON.stringify(this.role);
                axios.post('/role/addRole', "role="+role).then(res=>{
                    this.dialogFormVisible=false;
                    if(res){
                        this.$message({
                            type:'success',
                            message:"添加成功"
                        });
                    }else {
                        this.$message.error("添加失败");
                    }
                    this.getRolePage();
                });
            }
        }
    })
</script>
</html>